package com.dashu.core.controller;

import com.dashu.base.enums.ReplRyCode;
import com.dashu.core.dto.PageDTO;
import com.dashu.core.result.PageEnty;
import com.dashu.core.result.ResultBean;
import com.dashu.core.dto.company.CompanyClsDTO;
import com.dashu.core.service.CompanyClsService;
import com.dashu.core.vo.company.CompanyClsListVO;
import com.dashu.core.vo.company.CompanyClsVO;
import com.dashu.core.vo.UpdateEntityVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;


/**
 * @Author xys
 * @Date 2020/ReplRyCode.SUCCESS.getCode()ReplRyCode.SUCCESS.getCode()/ReplRyCode.SUCCESS.getCode()7 ReplRyCode.SUCCESS.getCode()5:2ReplRyCode.SUCCESS.getCode()
 */

@RequestMapping("dashu/companyCls")
@RestController
@Slf4j
@Api(value = "dashu/companyCls", tags = {"单位组织模块"}, description = "后台")
public class CompanyClsController {

    @Autowired
    private CompanyClsService companyClsService;
//    @Autowired
//    RestTemplate restTemplate;

    @PostMapping("/addCls")
    @ApiOperation(value = "组织添加", notes = "组织添加")
    public ResultBean<CompanyClsDTO> addCls(@RequestBody @Validated CompanyClsVO companyClsVO) {
        CompanyClsDTO companyDTO = companyClsService.existByCompanyClsName(companyClsVO.getClsName(), companyClsVO.getDepartmentId());
        if (companyDTO != null) {
            return new ResultBean<>(ReplRyCode.EXIST_COMPANY.getCode(), ReplRyCode.EXIST_COMPANY.getMessage(), null);
        }
        if (companyClsVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "组织添加", companyClsService.add(companyClsVO));
    }

    @PostMapping("/updataCls")
    @ApiOperation(value = "组织信息修改", notes = "组织信息修改")
    public ResultBean<CompanyClsDTO> updataCls(@RequestBody @Validated UpdateEntityVO<CompanyClsVO> updateEntityVO) throws Exception {
        if (updateEntityVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "组织信息修改", companyClsService.updataByCompanyClsId(updateEntityVO));
    }

    @PostMapping("/findBase")
    @ApiOperation(value = "父节点查询组织列表", notes = "父节点查询组织列表")
    public ResultBean<List<CompanyClsDTO>> findBase(@RequestBody @Validated PageEnty<Long> creteUserVo) {
        if (creteUserVo == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "父节点查询组织列表", companyClsService.findByCreteUser(creteUserVo));

    }

    @PostMapping("/findCls")
    @ApiOperation(value = "组织信息查询", notes = "组织信息查询")
    public ResultBean<CompanyClsDTO> findCls(long companyClsId) {
        if (companyClsId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "组织信息查询", companyClsService.findByCompanyClsId(companyClsId));

    }

    @DeleteMapping("/deleteCls")
    @ApiOperation(value = "删除组织信息", notes = "删除组织信息")
    public ResultBean<Boolean> deleteCls(long companyClsId) {
        if (companyClsId == 0) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "删除组织信息", companyClsService.deleteByCompanyClsId(companyClsId));

    }

    @PostMapping("/pageCompanyClsList")
    @ApiOperation(value = "班级信息列表", notes = "班级信息列表")
    public ResultBean<PageDTO<CompanyClsDTO>> pageCompanyClsList(@RequestBody @Validated PageEnty<CompanyClsListVO> clsListPageEntyVO) throws Exception {
        PageDTO<CompanyClsDTO> pageDTO = new PageDTO<>();
        if (clsListPageEntyVO == null) {
            return new ResultBean<>(ReplRyCode.DATA_NOT_NULL.getCode(), ReplRyCode.DATA_NOT_NULL.getMessage(), null);
        }
        long total = companyClsService.clsCount(clsListPageEntyVO);
        pageDTO.setTotal(total);
        pageDTO.setDataList(companyClsService.pageClsList(clsListPageEntyVO));
        return new ResultBean<>(ReplRyCode.SUCCESS.getCode(), "班级信息列表", pageDTO);

    }

}
